package xin.nick.demo.common;

import java.util.HashMap;

/**
 * @author Nick
 * @date 2021/5/22
 * @description
 */
public class R extends HashMap {

    private static final String KEY_CODE = "code";
    private static final String KEY_MSG = "msg";
    private static final String KEY_DATA = "data";

    private int code;
    private String msg;
    private Object data;

    public static String getKeyCode() {
        return KEY_CODE;
    }

    public static String getKeyMsg() {
        return KEY_MSG;
    }

    public static String getKeyData() {
        return KEY_DATA;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public R() {
        this.put(KEY_CODE, ResultCodeEnum.OK.code);
        this.put(KEY_MSG, ResultCodeEnum.OK.msg);
        this.put(KEY_DATA, null);
    }

    public R(int code, Object data) {
        this.put(KEY_MSG, ResultCodeEnum.OK.msg);
        this.put(KEY_CODE, code);
        this.put(KEY_DATA, data);
    }

    public R(ResultCodeEnum resultCodeEnum) {
        this.put(KEY_MSG, resultCodeEnum.msg);
        this.put(KEY_CODE, resultCodeEnum.code);
        this.put(KEY_DATA, null);
    }

    @Override
    public R put(Object key, Object value) {
        super.put(key, value);
        return this;
    }

    public static R ok() {
        return new R(ResultCodeEnum.OK);
    }
    public R ok(Object data) {
        R r = R.ok();
        r.put(KEY_DATA, data);
        return r;
    }

    public R fail() {
        return new R(ResultCodeEnum.SERVER_ERROR);
    }


}
